/*\
 * Copyright 2012 Rob van de Werken
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*/

package nl.RvdW.TVgids;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public final class Global {
	public static final String Name = "TVgids";
	public static final SimpleDateFormat dateformat = new SimpleDateFormat(
			"HH:mm");
	public static Context context;

	public static DatabaseHelper db;
	static TabHost tabs;
	static ViewPager viewpager;
	static PagerAdapter pageradapter;
	static List<Fragment> fragments = new Vector<Fragment>();

	// static TabsAdapter tabsadapter;

	// ////
	public static void addTab(Fragment frag, String identifier, String name) {
		fragments.add(frag);
		TabSpec ts = tabs.newTabSpec(identifier);
		// Attach a Tab view factory to the spec
		ts.setContent(new TabFactory(context));
		View v = LayoutInflater.from(context).inflate(R.layout.main_tab,null);
		((TextView)v.findViewById(R.id.main_tab_text)).setText(name);
		ts.setIndicator(v);
		Drawable icon = Global.getDrawable("channel_" + identifier);
		if (icon != null) {
			((ImageView) v.findViewById(R.id.main_tab_icon)).setImageDrawable(icon);
//			ts.setIndicator(name, icon);
		}
		tabs.addTab(ts);
	}
	
	// ////
	public static void showProgramme(Programme p) {
		AlertDialog dialog = new AlertDialog.Builder(Global.context)
				.setTitle(
						p.title + "\nvan " + Global.dateformat.format(p.start)
								+ " tot " + Global.dateformat.format(p.stop))
				.setMessage(p.desc + "\n\n" + p.category)
				.setIcon(Global.getDrawableId("channel_" + p.channel)).create();
		dialog.setButton("Back", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface alert, int id) {
				alert.cancel();
			}
		});

		dialog.show();
	}

	public static int getDrawableId(String name) {
		return context.getResources().getIdentifier(name, "drawable",
				context.getPackageName());
	}

	public static Drawable getDrawable(int id) {
		if (id == 0) {
			return null;
		}
		return context.getResources().getDrawable(id);
	}

	public static Drawable getDrawable(String name) {
		return getDrawable(getDrawableId(name));
	}

	public static boolean isOnline() {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}
}
